package com.example.demo.Service;


import com.example.demo.Entity.Shelf;
import com.example.demo.Entity.Warehouse;
import com.example.demo.Repository.ShelfRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ShelfServiceImpl implements ShelfService {

    private final ShelfRepo shelfRepo;

    @Autowired
    public ShelfServiceImpl(ShelfRepo shelfRepo) {
        this.shelfRepo = shelfRepo;
    }

    @Override
    public Shelf createShelf(Shelf shelf) {
        return shelfRepo.save(shelf);
    }

    @Override
    public Optional<Shelf> getShelfById(Long id) {
        return shelfRepo.findById(id);
    }

    @Override
    public List<Shelf> getAllShelves() {
        return shelfRepo.findAll();
    }

    @Override
    public Shelf updateShelf(Long id, Shelf shelf) {
        Optional<Shelf> existingShelfOptional = shelfRepo.findById(id);
        if (existingShelfOptional.isPresent()) {
            Shelf existingShelf = existingShelfOptional.get();
            existingShelf.setLocation(shelf.getLocation());
            existingShelf.setMaxCapability(shelf.getMaxCapability());
            existingShelf.setCurrentStorage(shelf.getCurrentStorage());
            existingShelf.setWarehouse(shelf.getWarehouse());
            return shelfRepo.save(existingShelf);
        } else {
            return null; // Or throw an exception
        }
    }

    @Override
    public boolean deleteShelf(Long id) {
        if (shelfRepo.existsById(id)) {
            shelfRepo.deleteById(id);
            return true;
        } else {
            return false; // Or throw an exception
        }
    }

    @Override
    public List<Shelf> findByWarehouse(Warehouse warehouse) {
        return shelfRepo.findByWarehouse(warehouse);
    }

    @Override
    public List<Shelf> findByLocation(String location) {
        return shelfRepo.findByLocation(location);
    }

    @Override
    public List<Shelf> findByMaxWeight(Double maxWeight) {
        return shelfRepo.findByMaxCapability(maxWeight);
    }

    @Override
    public List<Shelf> findByCurrentWeight(Double currentWeight) {

        return shelfRepo.findByCurrentStorage(currentWeight);
    }

    @Override
    public Shelf findByItemInWeight(List<Shelf> shelves, double Weight, double quantity) {
        double minRemainingCapacity = 1e9;
        Shelf suitableShelf = null;
        for (Shelf shelf : shelves) {
            double remainingCapacity = shelf.getMaxCapability() - shelf.getCurrentStorage();
            if (remainingCapacity >= Weight * quantity && remainingCapacity < minRemainingCapacity) {
                minRemainingCapacity = remainingCapacity;
                suitableShelf = shelf;
            }
        }
        return suitableShelf;
    }

    @Override
    public Shelf findByItemOutWeight(List<Shelf> shelves, double Weight, double quantity) {

        double maxRemainingCapacity = 0;
        Shelf suitableShelf = null;
        for (Shelf shelf : shelves) {
            if (shelf.getCurrentStorage() >= Weight * quantity && shelf.getCurrentStorage() > maxRemainingCapacity) {
                maxRemainingCapacity = shelf.getCurrentStorage();
                suitableShelf = shelf;
            }
        }
        return suitableShelf;
    }

    @Override
    public void saveShelf(Shelf shelf) {
        shelfRepo.save(shelf);
    }
}